//
//  YFLikeMeVC.m
//  project
//
//  Created by Winny Zhang on 2019/3/26.
//  Copyright © 2019 健健. All rights reserved.
//

#import "YFLikeMeVC.h"
#import "YFLikeMeCell.h"
#import "YFLikeModel.h"
#import "HJChatRoomVC.h"

#define JOURNEY_LIKE_TYPE_LIKE_ME (@"0")

@interface YFLikeMeVC () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, assign) NSInteger page;

@property (nonatomic, strong) NSArray <YFLikeRecordModel *> *likeArray;

/**
 TableView
 */
@property (weak, nonatomic) IBOutlet UITableView *tableView;
/**
 查看更多 Button
 */
@property (weak, nonatomic) IBOutlet UIButton *viewMoreButton;

@end

@implementation YFLikeMeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupViews];
    [self loadData];
}

- (void)setupViews {
    self.view.backgroundColor = [UIColor withRGB:243];
    [self setupTableView];
}

- (void)setupTableView {
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableFooterView= [UIView new];
    self.tableView.rowHeight = 97;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerCellByNibName:@"YFLikeMeCell"];
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, Height_TabBar+ Height_StatusBar, 0);
    self.tableView.emptyView = self.tipEmptyView;
    
    @weakify(self)
    self.tableView.refresh(^{
        @strongify(self)
        self.page = 1;
        [self loadDataWithPage:self.page];
    })
    .loadMore(^{
        @strongify(self)
        self.page += 1;
        [self loadDataWithPage:self.page];
    });
    
    self.tableView.mj_footer.hidden = YES;
}

- (void)loadData {
    self.likeArray = [NSArray array];
    [self.tableView.mj_header beginRefreshing];
}

#pragma  mark - UITableViewDataSource, UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _likeArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YFLikeMeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YFLikeMeCell" forIndexPath:indexPath];
    
    if (indexPath.row < _likeArray.count) {
        cell.likeModel = _likeArray[indexPath.row];
    }
    @weakify(self)
    cell.block = ^(YFLikeRecordModel * _Nonnull model) {
        LKLog(@"去聊");
        @strongify(self)
        HJChatRoomVC * chatRoomVC = [[UIStoryboard storyboardWithName:@"Chat" bundle:nil]instantiateViewControllerWithIdentifier:chatRoom];
        RCConversationModel * conversationModel = [[RCConversationModel alloc]init];
        conversationModel.conversationTitle = model.user.nickName;
        conversationModel.conversationType = ConversationType_PRIVATE;
        conversationModel.targetId = model.user.userId;
        chatRoomVC.conversationModel = conversationModel;
        [self.navigationController pushViewController:chatRoomVC animated:YES];
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"选中了 %ld", (long)indexPath.row);
}

#pragma mark - 按钮点击事件
- (IBAction)viewMoreButtonClicked:(UIButton *)sender {
    
}

#pragma mark - 网络请求
- (void)loadDataWithPage:(NSInteger)page {
    NSString *postString = [Api stringByAppendingString:TripLikeApi];
    NSDictionary *params = @{
                             @"tripId":_tripId,
                             @"pageCurrent": @(page),
                             @"type": JOURNEY_LIKE_TYPE_LIKE_ME,
                             };
    LKLog(@"喜欢我的参数：%@", params);
    
    @weakify(self)
    [HJHttpTool get:postString params:params success:^(HJResponeds *result) {
        @strongify(self)
        LKLog(@"喜欢我的：%@", result);
        YFLikeModel *model = [YFLikeModel yy_modelWithJSON:result.data];
        
        self.likeArray = self.likeArray.addArray(model.records, self.tableView.mj_header.refreshing);
        
        [self.tableView reloadData];
        [self.tableView endRefresh];
    
        if (model.pages == self.page) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
        self.tableView.mj_footer.hidden = self.likeArray.count == 0;
    } failure:^(id result) {
        LKLog(@"喜欢我的错误：%@", result);
        @strongify(self)
        [self.tableView endRefresh];
        if (self.likeArray.count == 0) {
            self.tipEmptyView.message = result;
            [self.tableView reloadData];
        } else {
            [YFHud showError:result];
        }
    }];
}


@end
